﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace Expressionista.UserControls
{
	/// <summary>
	/// Interaction logic for RegexView.xaml
	/// </summary>
	public partial class RegexView : UserControl
	{
		//public Regex Regex
		//{
		//    get
		//    {
		//        return getRegex();
		//    }
		//}

		public string RegexString
		{
			get
			{
				return RegexTB.Text;
			}
		}

		public RegexOptions RegexOptions
		{
			get
			{
				return getRegexOptions();
			}
		}

		private RegexOptions getRegexOptions()
		{
			RegexOptions options = RegexOptions.None;

			if (IgnoreCaseTB.IsChecked == true)
			{
				options = options | RegexOptions.IgnoreCase;
			}

			if (MultilineTB.IsChecked == true)
			{
				options = options | RegexOptions.Multiline;
			}

			if (ExplicitCaptureTB.IsChecked == true)
			{
				options = options | RegexOptions.ExplicitCapture;
			}

			if (CompiledTB.IsChecked == true)
			{
				options = options | RegexOptions.Compiled;
			}

			if (SinglelineTB.IsChecked == true)
			{
				options = options | RegexOptions.Singleline;
			}

			if (IgnorePatternWhitespaceTB.IsChecked == true)
			{
				options = options | RegexOptions.IgnorePatternWhitespace;
			}

			if (RightToLeftTB.IsChecked == true)
			{
				options = options | RegexOptions.RightToLeft;
			}

			if (ECMAScriptTB.IsChecked == true)
			{
				options = options | RegexOptions.ECMAScript;
			}

			if (CultureInvariantTB.IsChecked == true)
			{
				options = options | RegexOptions.CultureInvariant;
			}

			return options;
		}

		public void SetRegex(string reStr, RegexOptions options)
		{

			RegexTB.Text = reStr;

			IgnoreCaseTB.IsChecked = false;
			MultilineTB.IsChecked = false;
			ExplicitCaptureTB.IsChecked = false;
			CompiledTB.IsChecked = false;
			SinglelineTB.IsChecked = false;
			IgnorePatternWhitespaceTB.IsChecked = false;
			RightToLeftTB.IsChecked = false;
			ECMAScriptTB.IsChecked = false;
			CultureInvariantTB.IsChecked = false;

			if ((options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase)
			{
				IgnoreCaseTB.IsChecked = true;
			}

			if ((options & RegexOptions.Multiline) == RegexOptions.Multiline)
			{
				MultilineTB.IsChecked = true;
			}

			if ((options & RegexOptions.ExplicitCapture) == RegexOptions.ExplicitCapture)
			{
				ExplicitCaptureTB.IsChecked = true;
			}

			if ((options & RegexOptions.Compiled) == RegexOptions.Compiled)
			{
				CompiledTB.IsChecked = true;
			}

			if ((options & RegexOptions.Singleline) == RegexOptions.Singleline)
			{
				SinglelineTB.IsChecked = true;
			}

			if ((options & RegexOptions.IgnorePatternWhitespace) == RegexOptions.IgnorePatternWhitespace)
			{
				IgnorePatternWhitespaceTB.IsChecked = true;
			}

			if ((options & RegexOptions.RightToLeft) == RegexOptions.RightToLeft)
			{
				RightToLeftTB.IsChecked = true;
			}

			if ((options & RegexOptions.ECMAScript) == RegexOptions.ECMAScript)
			{
				ECMAScriptTB.IsChecked = true;
			}

			if ((options & RegexOptions.CultureInvariant) == RegexOptions.CultureInvariant)
			{
				CultureInvariantTB.IsChecked = true;
			}
		}

		public Regex GetRegex()
		{
			RegexOptions options = getRegexOptions();
			return new Regex(RegexTB.Text, options);
		}

		public RegexView()
		{
			InitializeComponent();
		}
	}
}
